/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
Ext.chart.Chart.CHART_URL = '../../../ext3/resources/charts.swf';
Ext.QuickTips.init();
var _pageSize = 20;
var _title = '监测点年数据';
var store;
var now = new Date();
var loadServlet = '../../../servlet/data/YsjLoad';

var tabs = new Ext.TabPanel({
    activeTab: 0,
    plain: true,
    enableTabScroll: true,
    defaults: {autoScroll: true},
    items: [{
            title: currentOrgName + '监测点年数据',
            id: 'main-tab',
            layout: "fit",
            tbar: new Ext.Toolbar([            
            {
                text: '环比数据',
                iconCls: 'icon-linkratio',
                handler: compareRangeData
            },
            {
                text: '合格率环比',
                iconCls: 'icon-cycleratio',
                handler: compareRange
            }
        ]),
        listeners: {
            'render': function() {
                tbarPoint.render(this.tbar);
            }
        }
        }
    ]
});

var hourSeries = [
    {
        yField: 'voltage',
        displayName: '电压值',
        style: {
            fillColor: 0xAAAAFF,
            borderColor: 0x3333FF,
            lineColor: 0x3333FF
        }
    }
];
var extraStyle = {
    xAxis: {
        majorGridLines: {color: 0x999999, size: 1}
    },
    yAxis: {
        titleRotation: -90
    },
    legend: {
        display: "right",
        padding: 5,
        spacing: 2,
        font: {color: 0x000000, family: "Arial", size: 12},
        border: {size: 1, color: 0x999999}
    }

};



function compareRangeData(){
    var record = checkboxSel.getSelected();
    if (checkboxSel.getSelections().length === 0) {
        Ext.Msg.alert("系统提示", "您还没有选择监测点");
        return;
    } else if (checkboxSel.getSelections().length > 1) {
        Ext.Msg.alert("系统提示", "只能选择一个记录");
        return;
    }
    var vid = record.get('name') + ' 年度数据';
    var pointId = record.get('id');
    
    var ysjRangeDataStore = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName,
        autoLoad:true,
        baseParams:{lock:lock,cmd:'nsjRangeData',pointId:pointId,start:0,limit:_pageSize,yearFrom:now.add(Date.YEAR,-1).format('Y'),yearTo:now.format('Y')}
    });
   
   var gridRangeData = new Ext.grid.GridPanel({        
        store: ysjRangeDataStore,        
        columns:columns ,
        loadMask: true,        
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: ysjRangeDataStore,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: '目前没有信息'
        })
    });  
    var tab = tabs.add({
        title: vid,
        closable: true,
        id: vid,
        layout: 'fit',
        tbar:[
            {
                text: "选择开始日期:", xtype: 'label', style: {marginLeft: '20px'}
            },
            {
                xtype: 'yearcombo',
                id: 'YearFromRangeData' + pointId,
                value:thisYear-1,                
                width: 80,
                listeners: {
                    select: function(c, date) {                        
                        ysjRangeDataStore.baseParams.yearFrom = c.getValue();
                    }
                }
            },
            
            {
                text: "选择结束日期:", xtype: 'label', style: {marginLeft: '20px'}
            },
            {
                xtype: 'yearcombo',
                id: 'YearToRangeData' + pointId,
                value:thisYear,                
                width: 80,
                listeners: {
                    select: function(c, date) {                        
                        ysjRangeDataStore.baseParams.yearTo = c.getValue();
                    }
                }
            },
            {
                xtype: 'button',
                text: '搜索',
                iconCls: 'icon-search',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {                    
                    ysjRangeDataStore.reload();
                }
            }
        ]
    });    
    Ext.getCmp(vid).add(gridRangeData);    
    tab.show();
}



function compareRange() {
    var record = checkboxSel.getSelected();
    
    if (checkboxSel.getSelections().length === 0) {
        Ext.Msg.alert("系统提示", "您还没有选择监测点");
        return;
    } else if (checkboxSel.getSelections().length > 1) {
        Ext.Msg.alert("系统提示", "只能选择一个记录");
        return;
    }
    var pointId= record.get('id');
    var vid = record.get('name') + ' 年度合格率曲线';
    
    var tipRenderer = function(chart, rec, index, series) {
        var yearInfo = '日期: ' + rec.get('date');
        var empInfo = '合格率' + ': '
                + rec.get('rate');
        return yearInfo + '\n' + empInfo;
    };
    var extraStyle = {
        xAxis: {
            majorGridLines: {
                color: 0x999999,
                size: 1
            }
        },
        yAxis: {
            titleRotation: -90
        }
    };    
    var YsjRateStore = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'date',
        totalProperty: 'count',
        autoLoad: true,
        fields: ['date','rate'],
        baseParams:{lock:lock,cmd:"nsjRangeData",yearFrom:now.add(Date.YEAR,-1).format('Y'),yearTo:now.format('Y'),pointId:record.get('id'),start:0,limit:99}
    });
    var dayFrom;
    var dayTo;
    var chartPanel = {
        xtype: 'linechart',
        store: YsjRateStore,
        xField: 'date',
        tipRenderer:tipRenderer,
        yField: 'rate',
        extraStyle: extraStyle,        
        xAxis: new Ext.chart.CategoryAxis({
            displayName: '日期',
            title: '日期'
        }),
        yAxis: new Ext.chart.NumericAxis({
            displayName: '电压合格率',
            title: '电压合格率',
            labelRenderer: Ext.util.Format.
                    numberRenderer('0,0.000')
        })
    };

    var tab = tabs.add({
        title: vid,
        //iconCls: 'icon-tab',
        //bodyStyle: 'padding: 5px',
        //html:'abc',
        tbar: [
            {
                text: "选择开始日期:", xtype: 'label', style: {marginLeft: '20px'}
            },
            {
                xtype: 'yearcombo',
                id: 'YearFromRange' + pointId,
                value:thisYear-1,       
                width: 80,
                listeners: {
                    select: function(c, date) {                        
                        ysjRangeDataStore.baseParams.yearFrom = c.getValue();
                    }
                }
            },           
            {
                text: "选择结束日期:", xtype: 'label', style: {marginLeft: '20px'}
            },
            {
                xtype: 'yearcombo',
                id: 'YearToRange' + pointId,
                value:thisYear,                
                width: 80,
                listeners: {
                    select: function(c, date) {                        
                        ysjRangeDataStore.baseParams.yearTo = c.getValue();
                    }
                }
            },
            {
                xtype: 'button',
                text: '搜索',
                iconCls: 'icon-search',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {                    
                    YsjRateStore.reload();
                }
            }],
        closable: true,
        id: vid,
        layout: 'fit'
    });
    
    Ext.getCmp(vid).add(chartPanel);    
    tab.show();
}


var checkboxSel = new Ext.grid.CheckboxSelectionModel();
var store;
function buildWindow() {
        store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName,
        autoLoad:true,
        baseParams:{lock:lock,cmd:'generalYear',start:0,limit:_pageSize,yearTo:thisYear}
    });   
    
    tbarPoint.insert(0, yearToCombo);
    tbarPoint.insert(1,lblYear);    
    
    var grid = new Ext.grid.GridPanel({
        //title: _title,
        store: store,
        sm: checkboxSel,
        columns: [checkboxSel].concat(columns),            
        loadMask: true,
        
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: '目前没有信息'
        })
    });    
    Ext.getCmp('main-tab').add(grid);
    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            tabs
        ]
    });
}
Ext.onReady(buildWindow);
